package login.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import domain.Manager;
import domain.Student;
import domain.Teacher;


/**
 * 主页处理Action
 * 
 * May 9, 2009
 *
 * @author Fabian
 *
 */
public class HomeAction extends Action {

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("user");
		if (obj != null) {
			if (obj instanceof Student) {				//学生主页
				request.setAttribute("role", 3);
				return mapping.findForward("goHome");
			} else if (obj instanceof Teacher) {		//老师主页
				request.setAttribute("role", 2);
				return mapping.findForward("goHome");
			} else if (obj instanceof Manager) {		//管理员主页
				request.setAttribute("role", 1);
				return mapping.findForward("goHome");
			} else {
				request.setAttribute("flag", 2);
				return mapping.findForward("fail");
			}
		} else {
			request.setAttribute("flag", 2);
			return mapping.findForward("fail");
		}
	}
	
}
